import axios from "axios"
import { Methods, TaskItem } from "./Store/WorkPanel"
import store from "./Store"

const productionMode = process.env.NODE_ENV === "production"

const Api = {
    mmsUrl: productionMode ? "/MMS/Api/" : "http://127.0.0.1:8080/MMS/Api/",
    // mmsUrl: "http://192.168.43.222:8080/MMS/Api",
    languageList(){
        return `Localization/list.json`
    },
    languageStrings(langCode) {
        return `Localization/${langCode}/strings/strings.json`
    },
    webServer: {
        webServerStatus(){
            return `WebServer/Status`
        },
        availableDataBases(){
            return `WebServer/AvailableDataBases`
        },
        createDataBase(){
            return `WebServer/CreateDataBase`
        },
        createTables(){
            return `WebServer/CreateTables`
        },
        setDataBaseConfig(){
            return `WebServer/SetDataBaseConfig`
        },
    },
    area: {
        importAreaDataFromLocal(){
            return "Area/ImportFromLocal"
        },
        /**
         * 
         * @param {String} keyWord 
         * @param {Number} layer 
         * @param {Number} from 
         * @param {Number} count 
         */
        searchArea(keyWord, layer = 0, from = 0, count = 100){
            return `Area/Search?KeyWord=${keyWord}&Layer=${layer}&From=${from}&Count=${count}`
        },
        /**
         * 
         * @param {Number} id 
         */
        getAreaFromID(id){
            return `Area/FromID?ID=${id}`
        },
        /**
         * 
         * @param {Number} id 
         */
        getChilds(id){
            return `Area/Childs?ID=${id}`
        },
    },
    application: {
        /**
         * 
         */
        initialize(){
            return `Application/Initialize`
        }
    }

}

class MyHttpClient{
    client = axios.create({
        timeout: 60000,
        baseURL: Api.mmsUrl
    })

    promisedOne(action, config){
        let task = new TaskItem("Http request")
        Methods.addTask(store, task)

        return new Promise((resolve, rejects) => {
            action(config).then(data => {
                Methods.removeTask(store, task)
                resolve(data)
            }).catch(exception => {
                Methods.removeTask(store, task)
                rejects(exception)
            })
        })
    }

    promisedTwo(action, url, config){
        let task = new TaskItem("Http request")
        Methods.addTask(store, task)

        return new Promise((resolve, rejects) => {
            action(url, config).then(data => {
                Methods.removeTask(store, task)
                resolve(data)
            }).catch(exception => {
                Methods.removeTask(store, task)
                rejects(exception)
            })
        })
    }

    promisedThree(action, url, data, config){
        let task = new TaskItem("Http request")
        Methods.addTask(store, task)

        return new Promise((resolve, rejects) => {
            action(url, data, config).then(data => {
                Methods.removeTask(store, task)
                resolve(data)
            }).catch(exception => {
                Methods.removeTask(store, task)
                rejects(exception)
            })
        })
    }

    request(config = {}){
        return this.promisedOne(this.client.request, config)
    }
    get(url, config = {}){
        return this.promisedTwo(this.client.get, url, config)
    }
    delete(url, config = {}){
        return this.promisedTwo(this.client.delete, url, config)
    }
    head(url, config = {}){
        return this.promisedTwo(this.client.head, url, config)
    }
    post(url, data, config = {}){
        return this.promisedThree(this.client.post, url, data, config)
    }
    put(url, data, config = {}){
        return this.promisedThree(this.client.put, url, data, config)
    }
    patch(url, data, config = {}){
        return this.promisedThree(this.client.patch, url, data, config)
    }
}

export {Api, MyHttpClient}